import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import * as api from '../api';

Vue.use(Router)

//用户登录页面
const Login = () => import('@/views/login/index')
//注册页面
const Register = () => import('@/views/register/index')

/* Layout */
import Layout from '@/layout'

const router = new Router({
  routes: [{
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/register',
      name: 'register',
      component: Register
    },
    {
        path: '/submitProcedure',
        name: 'submitProcedure',
        component: () => import('@/views/procedure/procedure'),
      },
    {
      path: '/404',
      component: () => import('@/views/404'),
      hidden: true
    },
    {
      path: '/',
      component: Layout,
      redirect: '/home',
      children: [{
        path: 'home',
        name: 'home',
        component: () => import('@/views/home/home'),
      }]
    },
    {
      path: '/dataStatis',
      component: Layout,
      redirect: '/dataStatis',
      children: [{
        path: 'dataStatis',
        name: 'dataStatis',
        component: () => import('@/views/home/dataStatis'),
        meta: {
        title: '数据统计',
        icon: 'el-icon-s-data',
        requireAuth:true //// 需要登录才能进入的页面可以增加一个meta属性
        }
      }]
    },
    {
      path: '/userMagage',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'userMagage',
      meta: {
        title: '用户管理',
        icon: 'el-icon-user-solid',
        requireAuth:true
      },
      children: [{
        path: 'userList',
        component: () => import('@/views/userManage/userManage'),
        name: 'userList',
        meta: {
          title: '用户列表',
          icon: 'el-icon-user',
          requireAuth:true
        }
      }]
    },
    {
      path: '/business',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'business',
      meta: {
        title: '商家管理',
        icon: 'el-icon-s-fold',
        requireAuth:true
      },
      children: [
        {
          path: 'basicsMsg',
          component: () => import('@/views/business/basicsMsg'),
          name: 'basicsMsg',
          meta: {
            title: '基础信息',
            icon: 'el-icon-s-tools',
            requireAuth:true
          }
        },
        {
        path: 'miniProgram',
        component: () => import('@/views/business/miniProgram'),
        name: 'miniProgram',
        meta: {
          title: '小程序管理',
          icon: 'el-icon-files',
          requireAuth:true
        }
      }]
    },
    {
      path: '/storeManage',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'storeManage',
      meta: {
        title: '门店管理',
        icon: 'el-icon-s-custom',
        requireAuth:true
      },
      children: [{
          path: 'store_list',
          component: () => import('@/views/storeManage/store_list'),
          name: 'store_list',
          meta: {
            title: '门店列表',
            icon: 'el-icon-coordinate',
            requireAuth:true,
            keepAlive: true,
            isBack: false,
          },
          children:[
            {
              alwaysShow: false,
              hidden:true,
              keepAlive:true,
              path: 'store_one',
              component: () => import('@/views/storeManage/children/store_one/store_one'),
              name: 'store_one',
              meta: {
                title: '管理',
                icon: 'el-icon-coordinate',
                requireAuth:true
              }
            },
            /*{
              alwaysShow: false,
              path: 'all_merchant',
              component: () => import('@/views/storeManage/children/store_one/all_merchant'),
              name: 'all_merchant',
              meta: {
                title: '所有门店',
                icon: 'el-icon-coordinate',
                requireAuth:true
              }
            },*/
            {
              alwaysShow: false,
              hidden:true,
              path: 'shopSortSon',
              component: () => import('@/views/storeManage/children/shopSortSon/shopSortSon'),
              name: 'shopSortSon',
              meta: {
                title: '商品分类子分类',
                icon: 'el-icon-coordinate',
                requireAuth:true
              }
            }
          ]
        },

      ]
    },
    {
      path: '/shopManage',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'shopManage',
      meta: {
        title: '商品管理',
        icon: 'el-icon-receiving',
        requireAuth:true
      },
      children: [
        {
          path: 'goods_list',
          component: () => import('@/views/shopManage/goods_list'),
          name: 'goods_list',
          meta: {
            title: '商品列表',
            icon: 'el-icon-s-opportunity',
            keepAlive: true,
            isBack: false,
            requireAuth:true
          }
        },
        {
          path: 'add_goods',
          component: () => import('@/views/shopManage/add_goods'),
          name: 'add_goods',
          hidden: true,
          meta: {
            title: '添加商品',
            requireAuth:true
          }
        },
        {
          path: 'edit_goods',
          component: () => import('@/views/shopManage/edit_goods'),
          name: 'edit_goods',
          hidden: true,
          meta: {
            title: '编辑商品',
            requireAuth:true
          }
        },
        {
          path: 'spu_list',
          component: () => import('@/views/shopManage/spu_list'),
          name: 'spu_list',
          hidden: true,
          meta: {
            title: '规格管理',
            icon: 'el-icon-s-check',
            requireAuth:true
          }
        },
        {
          path: 'apply_list',
          component: () => import('@/views/shopManage/apply_list'),
          name: 'apply_list',
          meta: {
            title: '商品审核',
            icon: 'el-icon-files',
            requireAuth:true
          }
        },
        {
          path: 'upload_goods',
          component: () => import('@/views/shopManage/upload_goods'),
          name: 'upload_goods',
          hidden: true,
          meta: {
            title: '导入商品',
            icon: 'el-icon-files',
            requireAuth:true
          }
        }
      ]
    },
    {
      path: '/orderManage',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'orderManage',
      meta: {
        title: '订单管理',
        icon: 'el-icon-document-copy',
        requireAuth:true
      },
      children: [
        {
        path: 'order_list',
        component: () => import('@/views/orderManage/order_list'),
        name: 'order_list',
        meta: {
          title: '订单列表',
          icon: 'el-icon-receiving',
          keepAlive: true,
          isBack: false,
          requireAuth:true
        },
        children:[{
              alwaysShow: false,
              hidden:true,
              path: 'order_detail',
              name: 'order_detail',
              component: () => import('@/views/orderManage/order_detail/order_detail'),
              meta: {
                title: '订单详细',
                icon: 'el-icon-receiving',
                requireAuth:true
              }
          }
        ]

        }]
    },
    {
      path: '/financeManage',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'financeManage',
      meta: {
        title: '财务管理',
        icon: 'el-icon-user-solid',
        requireAuth:true
      },
      children: [{
        path: 'water_bil',
        component: () => import('@/views/financeManage/water_bill'),
        name: 'water_bil',
        meta: {
          title: '交易流水',
          icon: 'el-icon-s-check',
          requireAuth:true
        }
      }]
    },
    {
      path: '/promotion',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'promotion',
      meta: {
        title: '促销管理',
        icon: 'el-icon-box',
        requireAuth:true
      },
      children: [{
        path: 'promotion_rules',
        component: () => import('@/views/promotion/promotion_rules'),
        name: 'promotion_rules',
        meta: {
          title: '促销规则',
          icon: 'el-icon-money',
          requireAuth:true
        }
      },
        {
          path: 'coupon_list',
          component: () => import('@/views/promotion/coupon_list'),
          name: 'coupon_list',
          meta: {
            title: '优惠券管理',
            icon: 'el-icon-copy-document',
            requireAuth:true
          }
        }]
    },
    {
      path: '/distributionManage',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'distributionManage',
      meta: {
        title: '分销管理',
        icon: 'el-icon-box',
        requireAuth:true
      },
      children: [{
        path: 'distributionRule',
        component: () => import('@/views/distributionManage/distributionRule'),
        name: 'distributionRule',
        meta: {
          title: '分销规则',
          icon: 'el-icon-money',
          requireAuth:true
        }
      },
        {
          path: 'distributor_list',
          component: () => import('@/views/distributionManage/distributor_list'),
          name: 'distributor_list',
          meta: {
            title: '分销商',
            icon: 'el-icon-copy-document',
            requireAuth:true,
            keepAlive: true,
            isBack: false,
          }
        },
        {
          path: 'distributor_detail',
          hidden:true,
          component: () => import('@/views/distributionManage/distributor_detail'),
          name: 'distributor_detail',
          meta: {
            title: '分销商详情',
            requireAuth:true
          }
        },
        {
          path: 'withdraw',
          component: () => import('@/views/distributionManage/withdraw/withdraw'),
          name: 'withdraw',
          meta: {
            title: '提现列表',
            icon: 'el-icon-copy-document',
            requireAuth:true
          }
        },
        {
          path: 'all_commission',
          component: () => import('@/views/distributionManage/all_commission'),
          name: 'all_commission',
          meta: {
            title: '佣金明细',
            icon: 'el-icon-s-opportunity',
            requireAuth:true
          }
        },
      ]
    },
    {
      path: '/procedureConfig',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'procedureConfig',
      meta: {
        title: '小程序配置',
        icon: 'el-icon-crop',
        requireAuth:true
      },
      children: [{
          path: 'banner',
          component: () => import('@/views/procedureConfig/banner'),
          name: 'banner',
          meta: {
            title: '轮播图',
            icon: 'el-icon-copy-document',
            requireAuth:true
          }
        },
        {
          path: 'functionBlock',
          component: () => import('@/views/procedureConfig/functionBlock'),
          name: 'functionBlock',
          meta: {
            title: '功能区',
            icon: 'el-icon-s-opportunity',
            requireAuth:true
          }
        },
        {
          path: 'division',
          component: () => import('@/views/procedureConfig/division'),
          name: 'division',
          meta: {
            title: '专区',
            icon: 'el-icon-c-scale-to-original',
            requireAuth:true
          }
        },
        {
          path: 'aboutUs',
          component: () => import('@/views/procedureConfig/aboutUs'),
          name: 'aboutUs',
          meta: {
            title: '关于我们',
            icon: 'el-icon-headset',
            requireAuth:true
          }
        }
      ]
    },
    {
      path: '/system',
      component: Layout,
      redirect: 'noRedirect',
      alwaysShow: true,
      name: 'system',
      meta: {
        title: '系统管理',
        icon: 'el-icon-setting',
        requireAuth:true
      },
      children: [{
          path: 'role_auth',
          component: () => import('@/views/system/role_auth'),
          name: 'role_auth',
          meta: {
            title: '角色权限',
            icon: 'el-icon-s-finance',
            requireAuth:true
          }
        },
        {
          path: 'administrators',
          component: () => import('@/views/system/administrators'),
          name: 'administrators',
          meta: {
            title: '管理员',
            icon: 'el-icon-coordinate',
            requireAuth:true
          }
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) {// 判断是否需要登录权限
    if (window.localStorage.getItem("review")==1) {// 判断是否登录
      next()  //有登录名称进行下一步路由
    } else {// 没登录则跳转到登录界面
      next({
        path: '/submitProcedure',
        //query: {redirect: to.fullPath} //这个会在地址栏上带上路径
      })
    }
  } else {
    next()
  }
})
/* {
    path: 'external-link',
    component: Layout,
    children: [{
      path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
      meta: {
        title: 'External Link',
        icon: 'link'
      }
    }]
  }, */

// 404 page must be placed at the end !!!
// {
//   path: '*',
//   redirect: '/404',
//   hidden: true
// }
// ),

// const createRouter = () => new Router({
//   // mode: 'history', // require service support
//   scrollBehavior: () => ({
//     y: 0
//   }),
//   routes: constantRoutes
// })
//
// const router = createRouter()
//
// // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// export function resetRouter() {
//   const newRouter = createRouter()
//   router.matcher = newRouter.matcher // reset router
// }
//
// async(to, from, next) => {
// })

export default router
